package com.hp.attendance.serlvet.history.InService;

import com.hp.attendance.dao.EmployeeBysDao;
import com.hp.attendance.entity.Department;
import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.Position;
import com.hp.attendance.service.EmployeeBysService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/addPreEmployee")
public class EmployeePreAddServlet extends HttpServlet {
    private EmployeeBysService employeeBysService = new EmployeeBysService();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //获取存employee对象的集合
            List<Employee> list = employeeBysService.getEmpLista();
            //创建一个同上述集合相同长度的数组
            int[] arr = new int[list.size()];
            //创建一个list集合
            ArrayList<Integer> empList = new ArrayList<Integer>();
            //遍历存employee对象的集合
            for (Employee employee : list) {
                //得到employee对象，获取employee_number
                Integer employee_number = employee.getEmployee_number();
                //将获取到的employee_number存入list集合
                empList.add(employee_number);
            }
            //遍历存employee_number的集合
            for (int i = 0; i < empList.size(); i++) {
                //将empList集合寸的值给到arr数组
                arr[i] = empList.get(i);
            }
            //冒泡排序
            int temp;
            for(int i=0;i<arr.length;i++){
                for(int j=i+1;j<arr.length;j++){
                    if(arr[j]>arr[i]){
                        temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                    }
                }
            }


            //回显员工编号
            int num=arr[0]+1;
            req.setAttribute("num",num);


            //显示部门下拉列表
            List<Department> departments = employeeBysService.getDepartment();
            req.setAttribute("departments",departments);

            //显示职称下拉列表
            List<Position> positions = employeeBysService.getPosition();
            req.setAttribute("positions",positions);

            //跳转
            req.getRequestDispatcher("/pages/employee_add.jsp").forward(req,resp);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
